package org.ruoyi.graph3.service;
// LLMEnhancedParser.java

import com.fasterxml.jackson.databind.ObjectMapper;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;

public class LLMEnhancedParser {
	private static final String API_KEY = "sk-63f4083825c4427c89c22c9e26091ad3";
	private static final String API_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1";

	public String enhanceContentAnalysis(String content) {
		try {
			HttpClient client = HttpClient.newHttpClient();

			Map<String, Object> requestBody = new HashMap<>();
			requestBody.put("model", "deepseek-v3.1");

			Map<String, String> message = new HashMap<>();
			message.put("role", "user");
			message.put("content", "请分析以下知识内容，提取关键概念和关系: " + content);

			requestBody.put("messages", new Map[]{message});
			requestBody.put("max_tokens", 500);

			ObjectMapper mapper = new ObjectMapper();
			String jsonBody = mapper.writeValueAsString(requestBody);

			HttpRequest request = HttpRequest.newBuilder()
				.uri(URI.create(API_URL))
				.header("Content-Type", "application/json")
				.header("Authorization", "Bearer " + API_KEY)
				.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
				.build();

			HttpResponse<String> response = client.send(request,
				HttpResponse.BodyHandlers.ofString());

			// 解析响应并返回增强内容
			return parseLLMResponse(response.body());

		} catch (Exception e) {
			e.printStackTrace();
			return content; // 失败时返回原内容
		}
	}

	private String parseLLMResponse(String response) {
		// 解析LLM响应，提取有用信息
		// 这里需要根据具体的API响应格式进行解析
		return response;
	}
}